package com.xgry.bpm.api.business.gateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.gateway.dao.TBGatewayDao;
import com.xgry.bpm.api.business.gateway.entity.TBGateway;
import com.xgry.bpm.api.business.gateway.form.TBGatewayForm;
import com.xgry.bpm.api.business.gateway.service.TBGatewayService;
import com.xgry.bpm.api.business.gateway.vo.TBGatewayVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * TBGateway服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-15
 */
@Service
        public class TBGatewayServiceImpl extends ServiceImpl<TBGatewayDao, TBGateway>implements TBGatewayService {

        @Override
        public int save(TBGatewayForm record){
    TBGateway data=this.convertDO(record);
        return baseMapper.insert(data);
        }

        @Override
        public int updateById(TBGatewayForm record){
        //修改时间
    TBGateway data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public TBGatewayVO selectById(String id){
        TBGateway data=baseMapper.selectById(id);
        TBGatewayVO vo=new TBGatewayVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<TBGatewayVO>selectAll(TBGatewayForm record){
    TBGateway data=this.convertDO(record);
        QueryWrapper<TBGateway>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBGateway>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<TBGatewayVO>selectPage(TBGatewayForm record){
        // form -> do 转换
    TBGateway data=this.convertDO(record);
        // 分页数据设置
        Page<TBGateway>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<TBGateway>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<TBGateway>list=baseMapper.selectPage(page,queryWrapper);
        IPage<TBGatewayVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<TBGateway>getPage(IPage<TBGateway>page,QueryWrapper<TBGateway>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }


        /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private TBGateway convertDO(TBGatewayForm form){
            TBGateway data=new TBGateway();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<TBGatewayVO>convert(List<TBGateway>list){
        List<TBGatewayVO> tBGatewayList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return tBGatewayList;
        }
        for(TBGateway source:list){
    TBGatewayVO target=new TBGatewayVO();
        BeanUtils.copyProperties(source,target);
    tBGatewayList.add(target);
        }
        return tBGatewayList;
        }

        }
